
// 接口地址：https://www.apifox.cn/apidoc/shared-36f0cbc0-9064-44bc-850a-805e263bf494/api-25827426
axios.defaults.baseURL = ' http://124.223.14.236:3001/api'

const qs = ele => document.querySelector(ele)

async function fn() {
    // 获取省数据
    const { data: { data } } = await axios.get('/city/province')
    // console.log(data);
    const provinceStr = data.map(item => {
        return `
        <option value="${item.province}">${item.name}</option>
        `
    }).join('')
    qs('#province').innerHTML = '<option value="">请选择省份</option>' + provinceStr
    qs('#province').value = 34

    //获取城市数据
    const { data: { data: city } } = await axios.get('/city/city', {
        params: {
            provinceCode: '34'
        }
    })
    // console.log(city);
    const cityStr = city.map(item => {
        return `
        <option value="${item.city}">${item.name}</option>
        `
    }).join('')
    qs('#city').innerHTML = '<option value="">请选择城市</option>' + cityStr
    qs('#city').value = '01'

    //获取区县数据
    const { data: { data: county } } = await axios.get('/city/area', {
        params: {
            provinceCode: '34',
            cityCode: '01'

        }
    })
    // console.log(county);
    const countyStr = county.map(item => {
        return `
        <option value="${item.area}">${item.name}</option>
        `
    }).join('')
    qs('#county').innerHTML = '<option value="">请选择区县</option>' + countyStr
    qs('#county').value = '02'
}
fn()


// 更换省份下拉框的数据
qs('#province').addEventListener('change', async function () {
    // 拿到当前选择的省份的编码
    console.log(this.value);
    // 获取城市数据
    const { data: { data: city } } = await axios.get('/city/city', {
        params: {
            provinceCode: this.value
        }
    })
    // console.log(city);
    const cityStr = city.map(item => {
        return `
        <option value="${item.city}">${item.name}</option>
        `
    }).join('')
    qs('#city').innerHTML = '<option value="">请选择城市</option>' + cityStr
    qs('#county').innerHTML = '<option value="">请选择区县</option>'
})

// 更换城市下拉框的数据
qs('#city').addEventListener('change', async function () {
    // 拿到当前城市的编码
    console.log(this.value);
    const { data: { data: county } } = await axios.get('/city/area', {
        params: {
            provinceCode: qs('#province').value,
            cityCode: this.value

        }
    })
    // console.log(county);
    const countyStr = county.map(item => {
        return `
        <option value="${item.area}">${item.name}</option>
        `
    }).join('')
    qs('#county').innerHTML = '<option value="">请选择区县</option>' + countyStr
})